package com.linchong.xshop.util;

import javax.servlet.http.HttpServletRequest;

/**
 * @author linchong
 * @desc 解析request的工具类
 * @email littlexbear@163.com
 * @create 2018-07-15 10:32
 * @CopyRight(C) https://blog.csdn.net/littlexbear
 **/
public class HttpServletRequestUtil {

    //处理整型值:String --> int
    public static int getInt(HttpServletRequest request,String key){
         try{
             return Integer.decode(request.getParameter(key));
         }catch(Exception e){
             return -1;
        }
    }

    //处理长整型值:String --> int
    public static long getLong(HttpServletRequest request,String key){
        try{
            return Long.valueOf(request.getParameter(key));
        }catch(Exception e){
            return -1;
        }
    }

    //处理double型值:String --> double
    public static Double getDouble(HttpServletRequest request,String key){
        try{
            return Double.valueOf(request.getParameter(key));
        }catch(Exception e){
            return -1d;
        }
    }

    //处理布尔值:String --> boolean
    public static Boolean getBoolean(HttpServletRequest request,String key){
        try{
            return Boolean.valueOf(request.getParameter(key));
        }catch(Exception e){
            return false;
        }
    }

    //获取String值
    public static String getString(HttpServletRequest request,String key){
        try{

            String result = request.getParameter(key);
            if(result!=null){
                result = result.trim();
            }
            if("".equals(result)){
                return null;
            }
            return result;
        }catch (Exception e){
            return null;
        }
    }
}
